package tech.edwardvan.designpattern.pattern.creational.prototype;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * 原型模式
 * <p>
 * 浅拷贝
 *
 * @author EdwardVan
 */
@Slf4j
public class PrototypeDemo {
    public static void main(String[] args) {
        Date date = DateUtil.date();
        Person person1 = new Person("EdwardVan", date);
        Person person2 = ObjectUtil.clone(person1);
        log.info("birthday的引用是否指向同一个对象:{}", person1.getBirthday() == person2.getBirthday());
    }

    /**
     * 人类
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    private static class Person implements Cloneable {
        /**
         * 姓名
         */
        private String name;
        /**
         * 生日
         */
        private Date birthday;

        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }
}
